package io.renren.service.impl;

import io.renren.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import io.renren.dao.EmpArchivesbaseDao;
import io.renren.entity.EmpArchivesbaseEntity;
import io.renren.service.EmpArchivesbaseService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Transactional(propagation= Propagation.REQUIRED)
@Service("empArchivesbaseService")
public class EmpArchivesbaseServiceImpl implements EmpArchivesbaseService {
	@Autowired
	private EmpArchivesbaseDao empArchivesbaseDao;
	
	@Override
	public EmpArchivesbaseEntity queryObject(Integer id){
		return empArchivesbaseDao.queryObject(id);
	}
	
	@Override
	public List<EmpArchivesbaseEntity> queryList(Map<String, Object> map){
		return empArchivesbaseDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return empArchivesbaseDao.queryTotal(map);
	}
	
	@Override
	public void save(EmpArchivesbaseEntity empArchivesbase){
		empArchivesbaseDao.save(empArchivesbase);
	}

	@Override
	public void update(EmpArchivesbaseEntity empArchivesbase){
		empArchivesbaseDao.update(empArchivesbase);
	}
	
	@Override
	public void delete(Integer id){
		empArchivesbaseDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		empArchivesbaseDao.deleteBatch(ids);
	}

	@Override
	public void saveBatch(List<EmpArchivesbaseEntity> list) {
		empArchivesbaseDao.saveBatch(list);
	}

	@Override
	public void passBatch(Integer[] ids,String status) {
		for(int i=0;i<ids.length;i++){
			String JobArchivesNo=null;
			if("1".equals(status)){
				EmpArchivesbaseEntity empArchivesbaseEntity = queryObject(ids[i]);
				if(empArchivesbaseEntity.getSettlementId()!=null&&!"".equals(empArchivesbaseEntity.getSettlementId())){
					String JobArchivesNoTop = empArchivesbaseDao.queryJobArchivesNo(empArchivesbaseEntity.getSettlementId());
					JobArchivesNo = getNewBatch(JobArchivesNoTop);
				}
			}
			empArchivesbaseDao.passBatch(ids[i],status,JobArchivesNo);
		}

	}

	@Override
	public void acceptance(Integer[] ids,String status) {
		for(int i=0;i<ids.length;i++){
			String quitArchivesNo=null;
			if("1".equals(status)){
				EmpArchivesbaseEntity empArchivesbaseEntity = queryObject(ids[i]);
				if(empArchivesbaseEntity.getSettlementId()!=null&&!"".equals(empArchivesbaseEntity.getSettlementId())){
					String quitArchivesNoTop = empArchivesbaseDao.queryQuitArchivesNo();
					quitArchivesNo = getNewBatch(quitArchivesNoTop);
				}
			}
			empArchivesbaseDao.acceptance(ids[i],status,quitArchivesNo);
		}

	}

	public EmpArchivesbaseEntity getRecordByPersonid(String empid){return empArchivesbaseDao.getRecordByPersonid(empid); }

	public String getNewBatch(String Batch){
		String NewBatch = null;
		if(Batch!=null&&!"".equals(Batch)){
			String[] str = Batch.split("-");
			int yesr = Integer.parseInt(str[0]);
			if(yesr==Integer.parseInt(DateUtils.getYear())){
				int no = Integer.parseInt(str[1]);
				if(no<9){
					NewBatch = DateUtils.getYear()+"-000"+(no+1);
				}else if(9<=no&&no<99){
					NewBatch = DateUtils.getYear()+"-00"+(no+1);
				}else if(99<=no&&no<999){
					NewBatch = DateUtils.getYear()+"-0"+(no+1);
				}else if(999<=no){
					NewBatch = DateUtils.getYear()+"-"+(no+1);
				}
			}else{
				NewBatch = DateUtils.getYear()+"-0001";
			}

		}else{
			NewBatch = DateUtils.getYear()+"-0001";
		}
		return NewBatch;
	}
}
